/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Aug 21, 2011, 3:46:36 PM
 */
package view;

import controller.AccountsController;
import controller.PropertiesUtility;
import javax.swing.JOptionPane;

/**
 *
 * @author TranDang
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
        start();
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        caption = new javax.swing.JLabel();
        userName = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        jtfUserName = new javax.swing.JTextField();
        jbLogin = new javax.swing.JButton();
        jbReset = new javax.swing.JButton();
        jpfPassword = new javax.swing.JPasswordField();
        lockIcon = new javax.swing.JLabel();
        jbConfig = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Crime File System");

        caption.setFont(new java.awt.Font("Courier New", 1, 24));
        caption.setText("SYSTEM LOGIN");

        userName.setFont(new java.awt.Font("Courier New", 0, 14));
        userName.setText("User Name:");

        password.setFont(new java.awt.Font("Courier New", 0, 14));
        password.setText("Password:");

        jtfUserName.setFont(new java.awt.Font("Courier New", 0, 14)); // NOI18N

        jbLogin.setText("Login");
        jbLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLoginActionPerformed(evt);
            }
        });

        jbReset.setText("Reset");
        jbReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbResetActionPerformed(evt);
            }
        });

        lockIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/lock.png"))); // NOI18N

        jbConfig.setText("Config Server");
        jbConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbConfigActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(caption, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(userName)
                            .addComponent(password))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jpfPassword)
                            .addComponent(jtfUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                    .addComponent(jbConfig))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbLogin)
                        .addGap(18, 18, 18)
                        .addComponent(jbReset))
                    .addComponent(lockIcon))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(caption, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userName)
                            .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password)
                            .addComponent(jpfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(lockIcon)))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbConfig)
                    .addComponent(jbLogin)
                    .addComponent(jbReset))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLoginActionPerformed
    String[] userProperties = new String[2];
    userProperties[0] = jtfUserName.getText();
    userProperties[1] = new String(jpfPassword.getPassword());
    if(isComponentValid(userProperties[0], userProperties[1])){
        if(AccountsController.login(userProperties[0], userProperties[1])){
            PropertiesUtility.createUserProperties(userProperties);
            initMainFrame(userProperties[0], userProperties[1]);
        }
    }
}//GEN-LAST:event_jbLoginActionPerformed

private void jbResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbResetActionPerformed
    jtfUserName.setText(null);
    jpfPassword.setText(null);
}//GEN-LAST:event_jbResetActionPerformed

private void jbConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbConfigActionPerformed
    new ServerConfig(this, true).setVisible(true);
}//GEN-LAST:event_jbConfigActionPerformed
    public static void start() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel caption;
    private javax.swing.JButton jbConfig;
    private javax.swing.JButton jbLogin;
    private javax.swing.JButton jbReset;
    private javax.swing.JPasswordField jpfPassword;
    private javax.swing.JTextField jtfUserName;
    private javax.swing.JLabel lockIcon;
    private javax.swing.JLabel password;
    private javax.swing.JLabel userName;
    // End of variables declaration//GEN-END:variables

    private boolean isComponentValid(String user, String pass) {
        if(user == null||user.isEmpty()){
            JOptionPane.showMessageDialog(this, "Enter your user name", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }else if (pass == null||pass.isEmpty()){
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }else {
            return true;
        }
    }

    private void initMainFrame(String user, String pass) {
        if(AccountsController.getPermission(user, pass).equals("Officer")){
            new OfficerMainFrame().setVisible(true);
        } else {
            new DirectorMainFrame().setVisible(true);
        }
        this.dispose();
    }
}
